<?php defined('SYSPATH') or die('No direct script access.');
/**
 * 权限资源管理
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: resource.php 1 2014-01-13 15:05:51Z zhouyl $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Controller_Admin_Manager_Resource extends Controller_Admin_Base
{

    /**
     * 资源列表
     */
    public function action_index()
    {
        $this->privilege('manager.resource.list');

        $this->view->resources = Model::factory('manager_resource')->all();
    }

    /**
     * 添加资源
     */
    public function action_add()
    {
        $this->privilege('manager.resource.add');

        $model = Model::factory('manager_resource');

        if ($this->request->is_post())
        {
            if ( ! $model->add($this->request->post()))
            {
                $this->view->errors = $model->errors('admin.manager.resource');
            }
            $this->render('admin/common/message');
        }
    }

    /**
     * 编辑资源
     */
    public function action_edit()
    {
        $this->privilege('manager.resource.edit');

        $res_id = $this->request->param('id');
        $model = Model::factory('manager_resource');
        $resource = $model->get($res_id);

        if ( ! $resource)
        {
            $this->message('admin.manager.resource.not_found')
                ->link(URL::site('admin/manager_resource'), 'admin.manager.resource.index_url')
                ->render();
        }

        if ($this->request->is_post())
        {
            if ( ! $model->edit($res_id, $this->request->post()))
            {
                $this->view->errors = $model->errors('admin.manager.resource');
            }
            $this->render('admin/common/message');
        }

        $this->view->set($resource);
    }

    /**
     * 删除资源
     */
    public function action_del()
    {
        $this->privilege('manager.resource.del');

        $res_id = $this->request->param('id');
        $model = Model::factory('manager_resource');

        if ( ! $model->del($res_id))
        {
            $this->view->errors = $model->errors('admin.manager.resource');
        }

        $this->render('admin/common/message');
    }

}
